package com.yue.yadmin.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.yue.yadmin.enums.EnumDictCode;
import com.yue.yadmin.enums.EnumMenuType;
import com.yue.yadmin.model.SysDict;
import com.yue.yadmin.model.SysMenu;
import com.yue.yadmin.service.SysDictService;
@Controller
@RequestMapping("/menu")
public class SysMenuController extends BaseController<SysMenu> {
	
	@Autowired
	SysDictService sysDictService;
	
	@RequestMapping("/menus")
	public ModelAndView menus(){
		ModelAndView modelAndView = new ModelAndView("menu/menus");
		modelAndView.addObject("menuTypes", EnumMenuType.getList());
		modelAndView.addObject("allMenus", getAllMenus());
		modelAndView.addObject("menuIcons",getMenuIcons());
		return modelAndView;
	}
	
	private List<Map<String,Object>> getMenuIcons(){
		SysDict sysDict = new SysDict();
		sysDict.setCode(EnumDictCode.MENU_ICON.getCode());
		List<SysDict> sysDicts = sysDictService.getList(sysDict);
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		String key = "key";
		String value = "value";
		for(int i=0,total=sysDicts.size();i<total;i++){
			sysDict =  sysDicts.get(i);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put(value, sysDict.getValue());
			map.put(key, sysDict.getRemark());
			list.add(map);
		}
		return list;
	}
	
	private List<Map<String,Object>> getAllMenus(){
		SysMenu sysMenu = new SysMenu();
		sysMenu.setType(EnumMenuType.MENU.getType());
		List<SysMenu> allMenus = service.getList(sysMenu);
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		String key = "key";
		String value = "value";
		Map<String,Object> firstMap = new HashMap<String, Object>();
		firstMap.put(key, "根节点");
		firstMap.put(value, 0);
		list.add(firstMap);
		for(int i=0,total=allMenus.size();i<total;i++){
			sysMenu =  allMenus.get(i);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put(value, sysMenu.getId());
			map.put(key, sysMenu.getName());
			list.add(map);
		}
		return list;
	}
	
}
